﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace Business.HotSite
{
    public static class MailUtility
    {
        public static MailMessage SendMail(string from, string[] to, string subject, string message)
        {
            var mailMessage = Mail(from, to, subject, message);
            mailMessage.IsBodyHtml = true;
            mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
            mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;

            return mailMessage;
        }
        public static MailMessage SendMail(string from, string[] to, string subject, string message, bool isHtml)
        {
            if (isHtml) 
            {
                message = "<div>" + message + "</div>";
            }
            var mailMessage = Mail(from, to, subject, message);
            mailMessage.IsBodyHtml = isHtml;
            return mailMessage;
        }
        /// <summary>
        /// from and to can be used in two formats: ericmagalhaes@gmail or ericmagalhaes@gmail.com,Eric Magalhaes
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="subject"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        private static MailMessage Mail(string from, string[] to, string subject, string message)
        {
            string fromMail = "";
            string fromName = "";

            string[] toMail = new string[to.Length];
            string[] toName = new string[to.Length];

            if (from.IndexOf(',') > 0)
            {
                fromMail = from.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0];
                fromName = from.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[1];
            }
            else
            {
                fromMail = from;
            }

            for (int i = 0; i < to.Length; i++)
            {
                if (to[i].IndexOf(',') > 0)
                {
                    toMail[i] = to[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0];
                    toName[i] = to[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[1];
                }
                else
                {
                    toMail[i] = to[i];
                }
            }


            var mailBody = new MailMessage();
            MailAddress fromMailAddress = null;

            if (!String.IsNullOrEmpty(fromName))
            {
                fromMailAddress = new MailAddress(fromMail, fromName);
            }
            else
            {
                fromMailAddress = new MailAddress(fromMail);
            }
            
            mailBody.From = fromMailAddress;

            for (int i = 0; i < to.Length; i++)
            {
                MailAddress toMailAddress = null;

                if (!String.IsNullOrEmpty(toName[i]))
                {
                    toMailAddress = new MailAddress(toMail[i], toName[i]);
                }
                else
                {
                    toMailAddress = new MailAddress(toMail[i]);
                }
                mailBody.To.Add(toMailAddress);
            }

            mailBody.Subject = subject;
            mailBody.Body = message;
            return mailBody;
        }
    }
}
